//<script type='text/javascript'>

win.push			= {};

win.push._proto_	= {
	update 	: function(vars) {
		xhttp.send('control.call.php', {trg:$(vars.id).ancestors()[1].id+".comment", wtype:'push', type:vars.page, tool:vars.tool, action:"shift", 'vars':vars});
	}
}



/////// TEXTFIELD OBJECT (auto-update)
var Textfield = function( vars, bare ) {
	
	obj = ( bare )? new Element( "div", { class:"container" }).update( vars.descr ) :
					new Element( "span" ).update( new Element( "span", { class:"name" }).update( vars.descr ));
	
	Object.extend( field = new Element( "input", { class: "textfield", type: "field", value: vars.values[ init.lang ]}), win.asset.textfield );
	obj.insert( field );
	field.vars = vars;
	
	field.observe( "focus", controls.bubble ).objtype = "text";

	return obj;
}

win.asset.textfield	= {

	reload		: function( oldlang, newlang ) {
		
		this.vars.values[ oldlang ] = this.value;
		this.value = ( this.vars.values[ newlang ] )? this.vars.values[ newlang ] : "";
	},
	
	
	update 		: function( string ) {
		
		this.value = this.vars.values[ init.lang ] = string;
		this.vars.langfocus = init.lang;
		
		options = { parameters: { set: Object.toJSON({ type:'text', action:'update', transfer_type:'js', vars:this.vars }) }};
		if( !this.vars.textid ) { 
			options.onSuccess = this.confirmNew.bind( this );
		} else {
			dashboard.comment( "Updated " + init.lang + " text" );
		}
		new Ajax.Request( 'control.call.php', options );
		
		//xhttp.send('control.call.php', { type:'text', action:'update', transfer_type:'push', vars:this.vars });
    },
    
    confirmNew	: function( obj ) {
    	this.vars.textid = Number( obj.responseText );
    	dashboard.comment( "Created text" );
    }
    
}

/////// TEXTFIELD OBJECT (auto-update)
var Searchfield = function( vars, bare ) {
	
	obj = ( bare )? new Element( "div", { class:"container" }).update( vars.descr ) :
					new Element( "span" ).update( new Element( "span", { class:"name" }).update( vars.descr ));
	
	Object.extend( field = new Element( "input", { class: "textfield", type: "field", value: vars.value }), win.asset.searchfield );
	obj.insert( field );
	field.vars = vars;
	
	field.observe( "focus", win.asset.searchfield.focus );
	field.observe( "blur", win.asset.searchfield.blur.bind( field, vars.type, vars.trg ));

	return obj;
}

win.asset.searchfield	= {

	reload		: function( oldlang, newlang ) {
		
		this.vars.values[ oldlang ] = this.value;
		this.value = ( this.vars.values[ newlang ] )? this.vars.values[ newlang ] : "";
	},
	
	focus		: function() {
	
		if( this.value == 'search term' )
			this.value = '';
	},
	
	blur		: function( type, trg ) {
		
		if( !this.value || this.value == 'search term' )
			this.value = 'search term';
		else 
			win.asset.searchfield.send( type, this.value, trg );
	},
	
	send		: function( type, value, trg ) {
	
		options = { parameters: { set: Object.toJSON({ type:type, action:'quest', transfer_type:'win', value: value, trg: trg })}};
		new Ajax.Request( 'control.call.php', options );
	}
    
}



/////// HTMLFIELD OBJECT (auto-update)
var Htmlfield = function( vars, bare ) {
	
	obj = ( bare )? new Element( "div", { class:"container" }).update( vars.descr ) :
					new Element( "span" ).update( new Element( "span", { class:"name" }).update( vars.descr ));

	Object.extend( field = new Element( "div", { class: "htmlfield" }).update( vars.values[ init.lang ]), win.asset.htmlfield );
	obj.insert( field );
	field.vars = vars;
	
	field.observe( "click", controls.bubble ).objtype = "html";

	return obj;
}

win.asset.htmlfield	= {

	reload		: function( oldlang, newlang ) {

		this.vars.values[ oldlang ] = this.innerHTML;
		this.update(( this.vars.values[ newlang ] )? this.vars.values[ newlang ] : "" );
	},
	
	
	update_html	: function( string ) {
		
		this.update( string ).vars.values[ init.lang ] = string;
		
		
		this.vars.langfocus = init.lang; this.vars.html = 1;
		
		options = { parameters: { set: Object.toJSON({ type:'text', action:'update', transfer_type:'js', vars:this.vars }) }};
		if( !this.vars.textid ) { 
			options.onSuccess = this.confirmNew.bind( this );
		} else {
			dashboard.comment( "Updated " + init.lang + " text" );
		}
		new Ajax.Request( 'control.call.php', options );
		
		//xhttp.send('control.call.php', { type:'text', action:'update', transfer_type:'push', vars:this.vars });
    },
    
    confirmNew	: function( obj ) {
    	this.vars.textid = Number( obj.responseText );
    	dashboard.comment( "Created text" );
    }
}


/////// Lightboxfield OBJECT (auto-update)
var Lightboxfield = function( vars, bare ) {
	
	/*obj = ( bare )? new Element( "div", { class:"container" }).update( vars.descr ) :
					new Element( "span" ).update( new Element( "span", { class:"name" }).update( vars.descr ));*/

	Object.extend( field = new Element("input", { class: "wide", type:"button", value:vars.descr }), win.asset.Lightboxfield );
	obj.insert( field );
	field.vars = vars;
		
	field.observe( "click", controls.bubble ).objtype = "lightbox";

	return obj;
}

win.asset.Lightboxfield	= {

	reload		: function( oldlang, newlang ) {

		this.vars.values[ oldlang ] = this.innerHTML;
		this.update(( this.vars.values[ newlang ] )? this.vars.values[ newlang ] : "" );
	},
	
	
	update_html	: function( string ) {
		
		this.update( string ).vars.values[ init.lang ] = string;
		
		this.vars.langfocus = init.lang; this.vars.html = 1;
		
		options = { parameters: { set: Object.toJSON({ type:'text', action:'update', transfer_type:'js', vars:this.vars }) }};
		if( !this.vars.textid ) {
			options.onSuccess = this.confirmNew.bind( this );
		} else {
			dashboard.comment( "Updated " + init.lang + " text" );
		}
		new Ajax.Request( 'control.call.php', options );
		//xhttp.send('control.call.php', { type:'text', action:'update', transfer_type:'push', vars:this.vars });
    },
    
    confirmNew	: function( obj ) {
    	this.vars.textid = Number( obj.responseText );
    	dashboard.comment( "Created text" );
    }
}



/////// AJX INPUT OBJECT (auto-update)
var Ajxfield = function (vars) {
	obj = new Element("div" , { class: "container" }).update( vars.descr );
	Object.extend( inp = new Element( "input", { class:"ajxfield", value: vars.value }), win.asset.ajxfield );
	inp.vars = vars;
	
	obj.insert( inp.observe('blur', inp.update ));
	if( vars.extend ) obj.insert( inp.extend_ico( vars.extend ));
	
	return obj;
}

win.asset.ajxfield	= {

	update 		: function( e ) {
		
		if( this.vars.value != this.value ) {
			this.vars.value = ( this.vars.func )? this.vars.func( e ) : this.value;
			xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
			this.vars.value = this.value;
		}
    },
    
    extend_ico	: function( ext ) {
    	
    	date = new Date();
    	
    	if( ext == "cal" ) return new Element( "div", { class:"ico_cal" }).update( new Element( "span" ).update( dashboard.month[ date.getMonth() ])).insert( new Element( "span", { class: "day" } ).update( date.getDate() )).observe( "click", function(){ console.log( "Foo." )});
    	
    	return "foo";
    	
    }
}
//Object.extend(win.asset.ajxfieldMK2, win.push._proto_);

/////// OPT (choose) OBJECT
var Opt = function (vars) {
	Object.extend(obj = new Element("div", {id:vars.id, class:"opt"}), win.asset.opt);
	Object.extend(obj.vars = vars, vars.meta);
	
	obj.appendChild(new Element("font", {id:vars.id+".value",class:"value"}).update(vars.value));
	obj.appendChild(new Element("div", {id:obj.id+".descr",class:"descr"}).update(vars.descr));
	obj.observe('click', function(){ this.pop(); });
	return obj;
}

win.asset.opt	= {
	pop 	: function() {
		$("dark").toggle();
		$("pop").update( new List({id:this.id+".lst", tool:this.vars.tool, descr:this.vars.descr, 'new':'add '+this.vars.type, meta:this.vars}) );
		
		xhttp.send('control.call.php', {trg:this.id+".lst", wtype:'push', type:this.vars.type, tool:this.vars.tool, action:"pull", 'vars':this.vars});
	},
	request	: function() {
		//
	}
}
Object.extend(win.asset.opt, win.push._proto_);

/////// SHIFT (I/O) OBJECT
var Shift = function ( vars ) {
	Object.extend(obj = new Element("div", { class:"shift" }), win.asset.shift );
	obj.vars = vars;
	
	obj.appendChild( choice = new Element("div", { class:"choice" }));
	obj.appendChild(new Element("font", { class:"descr" }).update( vars.descr ));
	obj.observe("click", function(){ this.send(); });
	if( obj.vars.value ) choice.addClassName( "active" );
	return obj;
}

win.asset.shift	= {
	send 	: function() {
		
		this.down( ".choice" ).toggleClassName( "active" );
		this.vars.value = ( this.vars.value )? 0: 1;
		xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", 'vars':this.vars });
	}
}
Object.extend(win.asset.shift, win.push._proto_);


/////// RANK (0/9) OBJECT
var Rank = function ( vars ) {
	Object.extend(obj = new Element( "div", { class:"rank" }), win.asset.rank );
	obj.vars = vars;
	
	obj.insert( field = new Element( "div", { class:"field" }).insert( obj.control = new Element( "div", { class:"control" })));
	obj.insert( new Element("span", { class:"descr" }).update( vars.descr )).pushrank( vars.value );
	
	field.observe( "click", obj.calc.bind( obj ));
	
	return obj;
}

win.asset.rank	= {
	
	calc	: function( e ) {
	
		var ox	= this.vars.value
		var x 	= e.screenX - e.currentTarget.viewportOffset().left;
		x		= Math.round( x/7 );
		
		this.pushrank( x );
		
		if( x != ox ) this.send( x );
	},
	
	pushrank	: function( n, obj ) {
		
		if( obj ) {
			Object.extend( this.vars, obj.vars );
			this.objref = obj;
		}
		
		this.vars.value = Number( n );
		this.control.setStyle({ width: ( this.vars.value*7 ) + "px" });
	},

	send 	: function( n ) {
		
		if( this.objref ) this.objref.vars.rank = n;
		
		this.vars.value = n;
		xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
	}
}


/////// ROLL (select) OBJECT
var Roll = function ( vars ) {
	
	obj = new Element( "div", { class:"container" }).update( vars.descr );
	
	Object.extend( field = new Element( "input", { type:"text", class:"ajxfield", value:vars.value }), win.asset.roll );
	obj.insert( field );
	
	field.observe( "click", controls.bubble ).objtype = "roll";
	//obj.insert( field.observe( "focus", field.send ));
	field.vars = vars;
	
	return obj;
}

win.asset.roll	= {
	
	send 	: function( n ) {
		
		if( this.vars.opts[ n ] && this.vars.opts[ n ] != this.vars.value ) {
			
			this.vars.value = this.vars.opts[ n ];
			xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
		}
	}
}


/////// SELECT OBJECT
var Select = function ( vars ) {
	obj = new Element( "div", { class:"container" }).update( vars.descr );

	if( vars.multiple ) {
		Object.extend( obj.del = new Element( "span", { class:"delete" }).update( ' - [x]' ), win.asset.select );
		//obj.insert( obj.del );
			
		Object.extend( obj.dup = new Element( "span", { class:"dupl" }).update( ' [+]' ), win.asset.select );
		//obj.insert( obj.dup );
		
		obj.insert( new Element( "div", { class: "actionate" }).insert( obj.dup ).insert( obj.del ));
	
		obj.del.observe( "click", obj.del.remover );
		obj.del.vars = vars;
	
		obj.dup.observe( "click", obj.dup.duplicate );
		obj.dup.vars = vars;
	}
	
	Object.extend( obj.field = new Element( "input", { type:"text", class:"select" }), win.asset.select );
	obj.insert( obj.field );
		
	obj.field.observe( "focus", controls.bubble ).objtype = "select";
	obj.field.vars = vars;
	
	if( vars.relation ) {
		obj.field.relation = vars.relation;
		obj.field.observe( "focus", obj.field.getRelation );
	
	} else if( !vars.opts )
		obj.field.observe( "focus", obj.field.get );
	
	if(  vars.opts )
		 vars.opts.each( function( vr ){ if( vr.key == vars.key ) obj.field.value = vr.value; }); 
	else if( vars.value )
		 obj.field.value = vars.value;
	
	return obj;
}

win.asset.select	= {
	
	get		: function() {

		options = { parameters: { set: Object.toJSON( { transfer_type:'js', type:this.vars.type, action:"referential", vars:this.vars } ) }, evalJSON: 'force', onSuccess : this.seed.bind( this )};
		new Ajax.Request( 'control.call.php', options );
	},
	
	getRelation	: function() {
	
		select = this.relation.down( ".select" );
		
		if( select )
			this.vars.relation = { parenttype: ( select.vars.ref )? select.vars.ref.parenttype : select.vars.objtype, parentid: select.vars.key };

		
		if( this.vars.relationhook != select.vars.key ) this.get();
		this.vars.relationhook = select.vars.key;
	},
	
	seed	: function( transport ) {
		
		transport = transport.responseJSON;
		this.vars.opts = transport.opts;
		
		$( "control_select" ).options.update();
		
		this.vars.opts.each( function( val ){
			this.options.insert( li = new Element( "li", ( val.key == obj.vars.key )? { class:"active" }: {} ).update( val.value ).observe( "click", this.change.bind( this )));
			li.vars = val;
		},  $( "control_select" ));
		
	},
	
	remover : function() {
	
		if( this.vars.multiple && this.up(3).select( 'li' ).length == 1 ) {
			this.vars.requestfunc = 'set_tag';
			this.up( 3 ).insert( new Element( 'li', { class: 'empty' }).update( new Select( this.vars )));
		}

		this.vars.requestfunc = 'remove_tag';
					 
		xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
		this.up(2).remove();
		
	},
	
	duplicate: function() {
		
		this.vars.ref.objid = 0;
		this.vars.key = 0;
		this.up( 3 ).insert( new Element( 'li', { class: 'empty' }).update( new Select( this.vars )));
	},
	
	send 	: function( vars ) {
		
		this.value = vars.value;
		
		if( this.vars.func )
			this.vars.requestfunc = this.vars.func;
		
		if( this.vars.ref )
			id = Number( vars.key );

		//if( this.vars.key != vars.key && ( !this.vars.ref || ( this.vars.ref.master && id != this.vars.ref.objid ) || ( !this.vars.ref.master && id != this.vars.ref.parentid ))) {
			 
			this.vars.key = vars.key;
		
			xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
			this.up(1).removeClassName( 'empty' );
		//}
		
		if( this.vars.multiple && this.up(2).select( '.empty' ).length == 0 ) {
			this.vars.key = 0;
			this.up( 2 ).insert( new Element( 'li', { class: 'empty' }).update( new Select( this.vars )));
		}
	}/*,
	
	send 	: function( vars ) {
		
		this.value = vars.value;
		
		if( this.vars.func ) //			this.vars.func( vars );
			this.vars.requestfunc = this.vars.func;
		
		if( this.vars.ref )
			id = Number( vars.key );

		//if( this.vars.key != vars.key && ( !this.vars.ref || ( this.vars.ref.master && id != this.vars.ref.objid ) || ( !this.vars.ref.master && id != this.vars.ref.parentid ))) {
			 
			this.vars.key = vars.key;
		
			xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
			this.up(1).removeClassName( 'empty' );
		//}
		
		if( this.vars.multiple && this.up(2).select( '.empty' ).length == 0 ) {
			this.vars.key = 0;
			this.up( 2 ).insert( new Element( 'li', { class: 'empty' }).update( new Select( this.vars )));
		}
		/*if( this.vars.func ) {
			res = this.vars.func( string, this );
			if( res ) string = res;
		}
		
		
		if( this.vars.ref )
			id = Number( vars.key );

		if( this.vars.key != vars.key && ( !this.vars.ref || ( this.vars.ref.master && id != this.vars.ref.objid ) || ( !this.vars.ref.master && id != this.vars.ref.parentid ))) {
			 
			this.vars.key = vars.key;
			xhttp.send( 'control.call.php', { transfer_type:'push', type:this.vars.type, action:"ajax", vars:this.vars });
		}
	}*/,
	
	renderOpts	: function( opts ) {
		
		opts.each( function( vr ){ opts[ vr.key ] = { key: vr.key, value: (( typeof vr.value.value !== "undefined" )? vr.value.value : vr.value ) }; });
		return opts;
	}
}



/////// BUTTON OBJECT
/* OLD BUTTON: 
var Button = function (vars) {
	Object.extend(obj = new Element("input", {id:vars.id, class:"wide", type:"button", value:vars.descr}), win.asset.button);
	Object.extend(obj.vars = vars, vars.meta);
	
	if( !vars.func ) vars.func = "send";
	obj.observe('click', obj[vars.func] );

	return obj;
}

win.asset.button	= {
	send 	: function() {
		this.vars.value = this.vars.meta.value = !this.vars.value;
		xhttp.send('control.call.php', {trg:this.id, wtype:'push', type:this.vars.type, tool:this.vars.tool, action:this.vars.action, 'vars':this.vars});
	},
	
	window	: function() {
		this.vars.vars = this.vars.meta; 
		$('column_right').insert( new Win( this.vars.id + ".win." + Math.random() ).request( this.vars ) );
	}
}
Object.extend(win.asset.button, win.push._proto_); */

/////// BUTTON OBJECT
var Button = function ( vars, listener, progress, onsucces ) {
	if( !vars.class ) vars.class = "wide";
	
	Object.extend( obj = new Element("input", { class: vars.class, type:"button", value:vars.descr}), win.asset.button );
	obj.vars		= vars;
	obj.listen 		= listener;
	obj.progress 	= progress;
	obj.succes 		= onsucces;
	
	if( !vars.func ) vars.func = "send";
	obj.observe('click', obj[vars.func] );

	return obj;
}

win.asset.button	= {
	send 	: function() {
		
		if( this.vars.buffer ) this.buffer();
		if( this.listen ) this.listen();
		//xhttp.send('control.call.php', {trg:this.id, transfer_type:"push", type:this.vars.type, action:this.vars.action, vars: this.vars });
		
		options = { onSuccess: this.succes, onInteractive: this.progress, parameters: { set: Object.toJSON({ transfer_type: ( this.vars.transfer_type )? this.vars.transfer_type: "push", trg:this.id, type:this.vars.type, action:this.vars.action, vars: this.vars }) }};
		new Ajax.Request( 'control.call.php', options );	
	},
	
	window	: function() {
		
		$('column_right').insert( new Win().request({ type: this.vars.type, action: this.vars.action, transfer_type: "win", vars: this.vars }) );
	},
	
	buffer	: function() {
		
		this.value = this.vars.buffer;
		this.disable();
	}
}


/////// ICON OBJECT
var Icon = function ( className, action ) {
	
	Object.extend( obj = new Element( "div", { class: "iconButton" }), win.asset.icon );
	obj.addClassName( className );
	obj.action = action;
	
	obj.observe('click', obj.listen.bind( obj ));

	return obj;
}

win.asset.icon	= {
	
	listen	: function() {
		
		this.addClassName( "progress" );
		this.action( this.succes.bind( this ));
	},
	
	succes	: function() {
		
		this.addClassName( "succes" );
		this.removeClassName( "progress" );
	}
}

/////// ELEMENTS MORE OBJECT
var More = function ( vars ) {
	Object.extend( obj = new Element( "div", { class: "more" }).update( vars.descr ), win.asset.more );
	obj.vars = vars;
	obj.ext = ( vars.extend == "cal" )? Calendar : Element;
		
	return obj.observe( "click", obj.call ); //dashboard.showhide
}

win.asset.more	= {
	call 	: function() {
		
		ext = dashboard.showextend( this );
		//ext.seedObject( this.vars );
		
	}
}

/////// ELEMENTS OVERVIEW OBJECT
var Elview = function ( vars ) {
	Object.extend( this.obj = new Element("span", { class: "elview" }), win.asset.elview );
	this.obj.vars = vars;
	
	this.obj.insert(( vars.action )? new Button( vars ) : new Element( "span", { class: "descr" }).update( vars.descr ));
	this.obj.insert( this.obj.opt = new Element( "span", { class: "opts" }));
	
	this.obj.seed( vars.opts );
	
	return this.obj;
}

win.asset.elview	= {
	
	seed	: function( opts ) {
		
		$H( opts ).each( function( val ) {
			this.opt.insert( el = new Element( "span" ).update( val.key ).observe( "click", this.send.bind( this )));
			el.vars = val.value;
		}.bind( this ));
		
	},
	
	send 	: function( e ) {
	
		elvars = e.currentTarget.vars;
		elvars.descr = this.vars.type + " " + e.currentTarget.innerHTML;
		$( 'column_left' ).insert( new Win().request({ type: this.vars.type, action: elvars.action, transfer_type: "win", vars: elvars }) );
	}
}


/////// HTML5 UPLOAD
var Dropper = function ( vars ) {
	Object.extend( this.obj = new Element("div", { id: "drop-area", class: "dropper" }), win.asset.dropper );
	this.obj.vars = vars;
	
	if( vars.descr ) this.obj.insert( new Element( "h1" ).update( vars.title ));
	if( vars.descr ) this.obj.insert( new Element( "span", { id: "status" }).update( vars.descr ));
	
	
	return this.obj.prepare();
}

win.asset.dropper	= {
	
	prepare		: function() {
			
			//tm = setTimeout( function(){ $j( this.init.bind( this )); }.bind( this ), 200 )
			tm = setTimeout( $j, 200, this.init.bind( this ));
			return this;
	},
	
	init		: function() {
	
		up.$jdrop = $j( this );//$j('#drop-area');
		
		up.$jdrop.bind('dragenter',up.enter);
		up.$jdrop.bind('dragleave',up.leave);
		up.$jdrop.bind('dragover',up.over);
		up.$jdrop.bind('drop',up.drop);
		
		$j('#status').click(up.cancel);
		
		up.xhr = new XMLHttpRequest();
		up.xhr.upload.addEventListener('progress', up.uploadProgress , false);
		up.xhr.upload.addEventListener('load', up.uploadLoaded , false);
		
	},
	
	end			: function( xhr ) {
		
		this.vars.succes( xhr );
		
	}
}

var up = {
	
	$jdrop :			null,
	queue :			[],
	processing :	null,
	uploading :		false,
	binaryReader :	null,
	dataReader :	null,
	xhr:			null,

	init : function() {
	
		
		up.$jdrop = $j( this );//$j('#drop-area');
		
		up.$jdrop.bind('dragenter',up.enter);
		up.$jdrop.bind('dragleave',up.leave);
		up.$jdrop.bind('dragover',up.over);
		up.$jdrop.bind('drop',up.drop);
		
		$j('#status').click(up.cancel);
		
		up.xhr = new XMLHttpRequest();
		up.xhr.upload.addEventListener('progress', up.uploadProgress , false);
		up.xhr.upload.addEventListener('load', up.uploadLoaded , false);
		
	},
	
	enter : function(e){
		$j(e.target).addClass('hover');
		return false;
	},
	
	leave : function(e){
		$j(e.target).removeClass('hover');
		return false;
	},
	
	over : function(e){
		return false;
	},
	
	drop : function(e){
		$j(e.target).removeClass('hover');
		
		var files = e.originalEvent.dataTransfer.files;
		for (var i = 0; i<files.length; i++) {
			var file = files[i];
			up.queue.push(file);
		}

		if(up.uploading == false) {
			up.uploading = true;
			up.process();
		}
		
		return false;
	},
	
	process : function() {
		up.processing = up.queue.shift();
		
		up.$jdrop.append('<div>'+up.processing.name+'</div>');
		
		if(window.FileReader) { //firefox 3.6, Chrome 6, Webkit

			if(up.processing.type.match(/image/gi) != null) { //is an image - read it
				up.dataReader = new FileReader();
				if(up.dataReader.addEventListener) { //firefox
					up.dataReader.addEventListener('loadend', up.binaryLoad, false);
					up.dataReader.addEventListener('error', up.loadError, false);
					up.dataReader.addEventListener('progress', up.loadProgress, false);
				} else { //chrome / webkit
					up.dataReader.onloadend = up.binaryLoad;
					up.dataReader.onerror = up.loadError;
					up.dataReader.onprogress = up.loadProgress;
				}
				up.dataReader.readAsDataURL(up.processing);
			}
			
			up.binaryReader = new FileReader();
			if(up.binaryReader.addEventListener) { //firefox
				up.binaryReader.addEventListener('loadend', up.binaryLoad, false);
				up.binaryReader.addEventListener('error', up.loadError, false);
				up.binaryReader.addEventListener('progress', up.loadProgress, false);
			} else { //chrome / webkit
				up.binaryReader.onloadend = up.binaryLoad;
				up.binaryReader.onerror = up.loadError;
				up.binaryReader.onprogress = up.loadProgress;
			}
			up.binaryReader.readAsBinaryString(up.processing);
			
			
		} else { // safari 5 + others?
		
			up.xhr.abort(); //make sure xhr is a new request
			up.xhr.open('POST', '/html5_upload.php?up=true', true);
		
			up.xhr.setRequestHeader('UP-FILENAME', up.processing.name);
			up.xhr.setRequestHeader('UP-SIZE', up.processing.size);
			up.xhr.setRequestHeader('UP-TYPE', up.processing.type);
			
			up.xhr.send(up.processing); 
			up.xhr.onload = up.onload;
		}
		
	},
	
	loadError : function(e) {
		switch(e.target.error.code) {
			case e.target.error.NOT_FOUND_ERR:
				alert('File Not Found!');
			break;
			case e.target.error.NOT_READABLE_ERR:
				alert('File is not readable');
			break;
			case e.target.error.ABORT_ERR:
			break; 
			default:
				alert('An error occurred reading this file.');
		};

	},
	
	loadProgress : function(e) {
		if (e.lengthComputable) {
			var percentage = Math.round((e.loaded * 100) / e.total);
			$j('#status').html('loaded: '+percentage+'%');
		}
	},
		
	binaryLoad : function(e) {
		
		var isimage = (up.processing.type.match(/image/gi)!=null);
		if( isimage && up.dataReader.readyState == 2 && up.$jdrop.find('div:last').css('background-image')=='none') {
			up.$jdrop.find('div:last').css('background-image','url(' + up.dataReader.result + ')');	
		}
		
		
		Object.extend( up.$jdrop[0].vars, { descr: '', title: '', auth: init.uid, account: init.account, up: true });
		getstring = Object.toQueryString( up.$jdrop[0].vars );
		
		if(isimage && up.dataReader.readyState == 2 && up.binaryReader.readyState == 2 || !isimage && up.binaryReader.readyState == 2 ) {
			
			up.xhr.abort(); //make sure xhr is a new request
			
			var binary = e.target.result;
			
			if(up.xhr.sendAsBinary != null) { //firefox
			
				up.xhr.open('POST', engine.path.local + 'control.upload.php?' + getstring, true);
	
				var boundary = 'xxxxxxxxx';
			    
				var body = '--' + boundary + "\r\n";  
				body += "Content-Disposition: form-data; name='upload'; filename='" + up.processing.name + "'\r\n";  
				body += "Content-Type: application/octet-stream\r\n\r\n";  
				body += binary + "\r\n";  
				body += '--' + boundary + '--';  
			    
			    up.xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
			    up.xhr.sendAsBinary(body);        	
			    
		    } else { //for browsers that don't support sendAsBinary yet
			 
			 	up.xhr.open('POST', engine.path.local + 'control.upload.php?base64=true&' + getstring, true);
			 
			 	up.xhr.setRequestHeader('UP-FILENAME', up.processing.name);
			 	up.xhr.setRequestHeader('UP-SIZE', up.processing.size);
			 	up.xhr.setRequestHeader('UP-TYPE', up.processing.type);
			 	
				up.xhr.send(window.btoa(binary)); 
		    }
			 
			up.xhr.onload = up.onload;
			
		}
	},
	
	uploadProgress : function(e) {
		if (e.lengthComputable) {
			var percentage = Math.round((e.loaded * 100) / e.total);
			$j('#status').html('uploaded: '+percentage+'%');
		}
	},
	
	uploadLoaded : function(e) {
		$j('#status').html('Uploaded: 100%');
	},
	
	onload : function (e) {
		if(up.queue.length > 0) {
			up.process();
		} else {
			up.uploading = false;
			$j('#status').html('Queue Uploaded');
			
			up.$jdrop[0].end( up.xhr )
			
		}
	},
	
	cancel : function(e) {
		if(up.dataReader) {
			up.dataReader.abort();
		}
		if(up.dataReader) {
			up.binaryReader.abort();
		}
		if(up.xhr) {
			up.xhr.abort();
		}
		up.uploading = false;
		up.queue = [];
		up.processing = null;
		$j('#status').html('Drag and Drop files to begin...');
		return false;
	}

}





